package com.google.code.swingonfire.utility;

import java.util.LinkedHashMap;

public class LRUCache<K, V> extends LinkedHashMap<K, V>
{

    private static final long serialVersionUID = -8101320478798764545L;

    private int capacity;

    public LRUCache(int capacity)
    {
        super(capacity + 1, 1.1f, true);

        this.capacity = capacity;
    }

    public int getCapacity()
    {
        return capacity;
    }

    /**
     * Sets the capacity and clears the cache
     * 
     * @param capacity the capacity
     */
    public void setCapacity(int capacity)
    {
        this.capacity = capacity;
        clear();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected boolean removeEldestEntry(java.util.Map.Entry<K, V> eldest)
    {
        return size() > capacity;
    }

}
